<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>mysql的root密码重置 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>mysql的root密码重置</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MySQL/">MySQL</a>
			</span>
		
	</div>

	

	
		<h2 id="mysql设置密码"><a href="#mysql设置密码" class="headerlink" title="mysql设置密码"></a>mysql设置密码</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># mysqladmin -uroot password 'yanyi'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：如果mysql已经有密码，该命令要加上 -p</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># mysqladmin -uroot -pyanyi password '123456'</span></div></pre></td></tr></table></figure>
<h2 id="碰到mysql-root密码忘记该如何做呢？"><a href="#碰到mysql-root密码忘记该如何做呢？" class="headerlink" title="碰到mysql root密码忘记该如何做呢？"></a>碰到mysql root密码忘记该如何做呢？</h2><h3 id="1-编辑mysql主配置文件my-cnf"><a href="#1-编辑mysql主配置文件my-cnf" class="headerlink" title="1.编辑mysql主配置文件my.cnf"></a>1.编辑mysql主配置文件my.cnf</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># vim /etc/my.cnf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 [mysql] 字段下添加参数</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">skip-grant</div></pre></td></tr></table></figure>
<h3 id="2-重启数据库服务"><a href="#2-重启数据库服务" class="headerlink" title="2.重启数据库服务"></a>2.重启数据库服务</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># service mysqld restart</span></div></pre></td></tr></table></figure>
<h3 id="3-进入mysql数据库（此时数据库不用授权）"><a href="#3-进入mysql数据库（此时数据库不用授权）" class="headerlink" title="3.进入mysql数据库（此时数据库不用授权）"></a>3.进入mysql数据库（此时数据库不用授权）</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># /usr/local/mysql/bin/mysql -uroot</span></div></pre></td></tr></table></figure>
<h3 id="4-修改相应的用户密码"><a href="#4-修改相应的用户密码" class="headerlink" title="4.修改相应的用户密码"></a>4.修改相应的用户密码</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用mysql库</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; use mysql;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更新一个表</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">mysql&gt; update user <span class="built_in">set</span> password=password(<span class="string">'yanyi'</span>) <span class="built_in">where</span> user=<span class="string">'root'</span>;</div><div class="line">mysql&gt; select * from user <span class="built_in">where</span> user=<span class="string">'root'</span>\G;</div><div class="line">mysql&gt; flush privileges;</div><div class="line">mysql&gt; quit;</div></pre></td></tr></table></figure>
<h3 id="5-修改-etc-my-cnf去掉skip-grant，重启mysql服务"><a href="#5-修改-etc-my-cnf去掉skip-grant，重启mysql服务" class="headerlink" title="5.修改/etc/my.cnf去掉skip-grant，重启mysql服务"></a>5.修改/etc/my.cnf去掉skip-grant，重启mysql服务</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># vim /etc/my.cnf</span></div><div class="line">[root@lamp ~]<span class="comment"># service mysqld restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样就可以用新密码登录了</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/mysql%E7%9A%84root%E5%AF%86%E7%A0%81%E9%87%8D%E7%BD%AE/01.png?raw=true" alt=""></figure></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/MySQL/9. mysql的root密码重置/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
